package com.lzy.system.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.conf.JSONResult;
import com.lzy.system.business.dto.SysBusGroupAddDTO;
import com.lzy.system.business.dto.SysBusGroupOrderDTO;
import com.lzy.system.business.dto.SysBusGroupUpdateDTO;
import com.lzy.system.business.dto.convert.SysBusGroupConvert;
import com.lzy.system.business.entity.SysBusGroup;
import com.lzy.system.business.service.ISysBusGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 业务模型分组 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/business/sys-bus-group")
@Api(tags = "业务模型分组")
public class SysBusGroupController {
    private ISysBusGroupService iSysBusGroupService;
    private SysBusGroupConvert sysBusGroupConvert;

    @Autowired
    public SysBusGroupController(ISysBusGroupService iSysBusGroupService, SysBusGroupConvert sysBusGroupConvert) {
        this.iSysBusGroupService = iSysBusGroupService;
        this.sysBusGroupConvert = sysBusGroupConvert;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ApiOperation("获取业务模型分组 列表")
    public JSONResult getList(
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) {
        IPage<SysBusGroup> page_obj = iSysBusGroupService.page(new Page<SysBusGroup>(page, limit));
        return JSONResult.ok(page_obj);
    }

    @RequestMapping(value = "/getListAll", method = RequestMethod.POST)
    @ApiOperation("获取业务模型分组 列表")
    public JSONResult getListAll(
    ) {
        return JSONResult.ok(iSysBusGroupService.list(
                new QueryWrapper<SysBusGroup>() {{
                    orderByAsc("order_index");
                    orderByAsc("create_time");
                }}

        ).stream().map(item -> {
            return sysBusGroupConvert.entityToVo(item);
        }));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加业务模型分组")
    public JSONResult add(
            @ApiParam("业务模型分组") @RequestBody @Validated SysBusGroupAddDTO data
    ) {
        SysBusGroup save_data = sysBusGroupConvert.addDtoToEntity(data);
        iSysBusGroupService.save(save_data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改业务模型分组")
    public JSONResult update(
            @ApiParam("业务模型分组") @RequestBody @Validated SysBusGroupUpdateDTO data
    ) {
        SysBusGroup save_data = sysBusGroupConvert.updateDtoToEntity(data);
        iSysBusGroupService.updateById(save_data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/saveOrderIndex", method = RequestMethod.POST)
    @ApiOperation("修改业务模型分组排序")
    public JSONResult saveOrderIndex(
            @ApiParam("业务模型分组") @RequestBody @Validated List<SysBusGroupOrderDTO> data
    ) {
        iSysBusGroupService.saveOrderIndex(data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除业务模型分组")
    public JSONResult delete(
            @ApiParam("ids") @RequestBody @Validated List<String> ids
    ) {
        iSysBusGroupService.removeByIds(ids);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation("获取业务模型分组详情")
    public JSONResult get(
            @ApiParam("id") @PathVariable(value = "id") @NotBlank(message = "id不能为空") String id
    ) {
        return JSONResult.ok(iSysBusGroupService.getById(id));
    }
}
